package com.itheima.test16;


import java.util.Comparator;
import java.util.TreeSet;

public class StudentDemo {
    public static void main(String[] args) {
        TreeSet<Student> set = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                // o1表示要添加的元素,o2表示已经在集合中的元素
                int result = o2.getAge() - o1.getAge();
                result = result == 0 ? o1.getId().compareTo(o2.getId()) : result;
                return result;
            }
        });

        Student s1 = new Student("张三", 18, "heima001");
        Student s2 = new Student("李四", 18, "heima002");
        Student s3 = new Student("王五", 17, "heima003");
        set.add(s1);
        set.add(s2);
        set.add(s3);

        System.out.println(set);

    }
}
